package com.easyui.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.easyui.bean.*;
import com.easyui.common.*;

public class GetTOProjectServlet extends HttpServlet {

	public GetTOProjectServlet() {}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/json;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		List<Project> plist = null;
		List<Map> initList = null;
		JSONObject jsonAll = null;
		JSONArray jsonProject = null;
		
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));//page和rows需要返回
		String orderBy = request.getParameter("order");//排序方式,不用返回
		String sortName = request.getParameter("sort");//排序字段,不用返回
		
		//查询条件
		String pname = request.getParameter("pname");
		String pubname = request.getParameter("pubname");
		String stimef = request.getParameter("stimef");
		String stimet = request.getParameter("stimet");
		String progress = request.getParameter("progress");
		String etimef = request.getParameter("etimef");
		String etimet = request.getParameter("etimet");
		
		int totalRecords = 0;
		jsonAll= new JSONObject();//装所有信息，包括total参数
		SelProject sp = new SelProject();
		
		HttpSession session = request.getSession(); //获的session
		String role = (String)session.getAttribute("role");
		String tno = (String)session.getAttribute("userID");
		
		if(role != null && role.trim().equals("teacher")) {//登录身份为老师
			if(tno != null && !tno.trim().equals("")) {//获取到该老师工号
				Project p = sp.getProject(pname, pubname, stimef, stimet, progress, etimef, etimet);
				initList = sp.getTOwnProjects(tno, orderBy, sortName, p);//返回所有老师信息
				plist = sp.getTOSpeProjects(initList, tno, page, rows, orderBy, sortName, p);
			}
		}
		
		if (plist != null && plist.size() > 0) {
			jsonProject = new JSONArray();//装学生信息
			for (int i = 0; i < plist.size(); i++) {
				Project p = (Project)plist.get(i);
				
				JSONObject j = new JSONObject();
				j.put("pno", p.getPno());
				j.put("pname", p.getPname());
				j.put("stime", p.getStime());
				j.put("etime", p.getEtime());
				j.put("pubname", p.getPubname());
				j.put("progress", p.getProgress());
				j.put("discuss", "进入");
				j.put("show", "进入");
				j.put("change", "");
				jsonProject.add(j);
			}
			
			totalRecords = initList.size();
			jsonAll.put("total", totalRecords);
			jsonAll.put("rows", jsonProject);
		} else {
			totalRecords = 0;
			jsonAll.put("total", totalRecords);
			jsonAll.put("rows", "");
		}
		
		out.print(jsonAll);
		out.flush();
		out.close();
	}


}
